package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;


    /**
     * 添加班级
     * @param clazz
     */
    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(clazz.getCreateTime());
        clazzMapper.insert(clazz);
    }

    /**
     * 查询所有班级
     * @return
     */
    @Override
    public List<Clazz> list() {
        List<Clazz> clazzList = clazzMapper.selectAll();
        return clazzList;
    }
    /**
     * 班级分页查询
     * @param clazzQueryParam
     * @return
     */
    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());
        Page<Clazz> pageInfo = clazzMapper.list(clazzQueryParam);
        // 判断状态
        LocalDate now = LocalDate.now();
        for (Clazz clazz : pageInfo.getResult()) {
            if (now.isBefore(clazz.getBeginDate())) {
                clazz.setStatus("未开课");
            } else if (now.isAfter(clazz.getEndDate())) {
                clazz.setStatus("已结课");
            }
        }
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public Clazz getById(Integer id) {
       return clazzMapper.getById(id);
    }

    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> findAll() {
        return  clazzMapper.findAll();
    }



}
